package com.kaer.plugin.utils;

import android.annotation.SuppressLint;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 时间转换工具
 * @author Wxcily
 * @date 2014-11-17
 */
public class TimeUtils {

	private static String mYear;
	private static String mMonth;
	private static String mDay;
	private static String mWay;


	private TimeUtils(){};

	public static String getTimeDescription(long timestamp) {
		long currentSeconds = System.currentTimeMillis() / 1000;
		long timeGap = currentSeconds - timestamp;// 与现在时间相差秒数
		String timeStr = null;
		if (timeGap > 24 * 60 * 60) {// 1天以上
			timeStr = timeGap / (24 * 60 * 60) + "天前";
		} else if (timeGap > 60 * 60) {// 1小时-24小时
			timeStr = timeGap / (60 * 60) + "小时前";
		} else if (timeGap > 60) {// 1分钟-59分钟
			timeStr = timeGap / 60 + "分钟前";
		} else {// 1秒钟-59秒钟
			timeStr = "刚刚";
		}
		return timeStr;
	}

	public static String getTime() {
		return getTime("yyyy-MM-dd HH:mm:ss");
	}

	public static String getTime(String format) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
		return sdf.format(date);
	}

	@SuppressLint("SimpleDateFormat") 
	public static String getTime(long timestamp) {
		Date date = new Date(timestamp * 1000L);
		String strs = "";
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			strs = sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strs;
	}


	public static String weekStringDate(Calendar dataCalender){
//		final Calendar c = Calendar.getInstance();
//		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		mWay = String.valueOf(dataCalender.get(Calendar.DAY_OF_WEEK));
		if("1".equals(mWay)){
			mWay ="天";
		}else if("2".equals(mWay)){
			mWay ="一";
		}else if("3".equals(mWay)){
			mWay ="二";
		}else if("4".equals(mWay)){
			mWay ="三";
		}else if("5".equals(mWay)){
			mWay ="四";
		}else if("6".equals(mWay)){
			mWay ="五";
		}else if("7".equals(mWay)){
			mWay ="六";
		}
		return "星期"+mWay;
	}


	public static String getPreviousMonth_timestamp(){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -1);    //得到前一个月
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH)+1;
		int day=calendar.get(Calendar.DAY_OF_MONTH);
//		String resultStr=String.format("%04d%02d%02d", year,month,day);
//		System.out.println("[debuginfo]:"+resultStr);

		String milllong=String.valueOf(calendar.getTimeInMillis());
//		System.out.println("[debuginfo]:"+milllong);
		return milllong;
	}

	/**
	 *
	 * @param index  after positive index or before negative Math.abs(index)
     * @return timeInMillions
     */
	public static String getDaysBeforeOrAfter(int index) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, index);    //得到前一个月
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
//		String resultStr=String.format("%04d%02d%02d", year,month,day);
//		System.out.println("[debuginfo]:"+resultStr);
		String milllong = String.valueOf(calendar.getTimeInMillis());
//		System.out.println("[debuginfo]:"+milllong);
		return milllong;
	}
}
